/**
 *      Created by 段新瑞 on 2017/4/7.
 *      Copyright © 2017年 联壁. All rights reserved.
 *           ┏┓       ┏┓
 *          ┏┛┻━━━━━━━┛┻┓
 *          ┃           ┃
 *          ┃     ━     ┃
 *          ┃  ┳┛   ┗┳  ┃
 *          ┃           ┃
 *          ┃     ┻     ┃
 *          ┃           ┃
 *          ┗━┓       ┏━┛
 *            ┃       ┃
 *            ┃       ┃
 *            ┃       ┗━━━┓
 *            ┃           ┣━┓
 *            ┃           ┏━┛
 *            ┗┓┓┏━━━━━┳┓┏┛
 *             ┃┫┫     ┃┫┫
 *             ┗┻┛     ┗┻┛
 *
 *      LBConfigModel.m
 *      LandCredit
 **/

#import "ZYUserInfoModel.h"
#import <objc/runtime.h>
#import "ZYHomeVC.h"
#import "AppDelegate.h"
#import "ZYLoginViewController.h"


#define NSUserDefaultsName  @"userModel"
static ZYUserInfoModel *_sharedUserInfoModel = nil;

@interface ZYUserInfoModel()<NSCoding>

@end

@implementation ZYUserInfoModel

MJCodingImplementation

+ (instancetype)sharedInfoModel {
    if (_sharedUserInfoModel == nil) {
        _sharedUserInfoModel = [[ZYUserInfoModel alloc] init];
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSData *data = [user objectForKey:NSUserDefaultsName];
        if (data != nil) {
            _sharedUserInfoModel = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        }
    }
    return _sharedUserInfoModel;
}

+ (void)requestUserModel {
    if ([LBLimitInput isEmpty:[ZYUserInfoModel sharedInfoModel].userModel.userId]) return;
    NSLog(@"%@", @"已登录");
    
    NSDictionary *dict = @{@"userId": ZYUserInfoModel.sharedInfoModel.userModel.userId};
    [[DataManager manager] postGetTimKeyParam:dict block:^(XRCommonModel *model, NSError *error) {
        [ZYHud hideHud];
        if (model.isSuccess) {
            ZYRequestUserInfoModel *userModel = [ZYRequestUserInfoModel mj_objectWithKeyValues:model.data];
            ZYUserInfoModel.sharedInfoModel.userModel = userModel;
            [ZYUserInfoModel.sharedInfoModel saveUserModel];
            
            /// 已登录
            RTContainerNavigationController *navVc = [[RTContainerNavigationController alloc] initWithRootViewController:[ZYHomeVC new]];
            [(AppDelegate *)[UIApplication sharedApplication].delegate rootViewController:navVc];
        } else {
            if (![LBLimitInput isEmpty:model.error]) {
                [ZYHud showErrorText:model.error];
            }
            /// 已登录
            RTContainerNavigationController *navVc = [[RTContainerNavigationController alloc] initWithRootViewController:[ZYLoginViewController new]];
            [(AppDelegate *)[UIApplication sharedApplication].delegate rootViewController:navVc];
        }
    }];
    
}

+ (void)saveUserModel {
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:[ZYUserInfoModel sharedInfoModel]];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:data forKey:NSUserDefaultsName];
    [user synchronize];
}

- (void)saveUserModel {
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:data forKey:NSUserDefaultsName];
    [user synchronize];
}

+ (void)clean {
//    _sharedUserInfoModel.userId         = nil;
//    _sharedUserInfoModel.managerId      = nil;
//    _sharedUserInfoModel.userId         = nil;
//    _sharedUserInfoModel.companyId      = nil;
//    _sharedUserInfoModel.phone          = nil;
//    _sharedUserInfoModel.userName       = nil;
//    _sharedUserInfoModel.idCard         = nil;
//    _sharedUserInfoModel.gender         = nil;
//    _sharedUserInfoModel.role           = nil;
//    _sharedUserInfoModel.userType       = nil;
//    _sharedUserInfoModel.token          = nil;
//    _sharedUserInfoModel.timKey         = nil;
    _sharedUserInfoModel.userModel      = nil;
    
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:[ZYUserInfoModel sharedInfoModel]];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:data forKey:NSUserDefaultsName];
    [user synchronize];
}

- (void)clean {
    [ZYUserInfoModel clean];
}

+ (BOOL)isLogin {
    return ZYUserInfoModel.sharedInfoModel.userModel.userId ? YES : NO;
}

/// 是否店长
+ (BOOL)isManager {
    return [ZYUserInfoModel.sharedInfoModel.userModel.role isEqualToString:@"1"] ? YES : NO;
}

@end
